package com.dahuyou.tryy.too.simulate.interpreter.runtime.area;

import com.dahuyou.tryy.too.simulate.interpreter.runtime.area.heap.methodarea.Method;

/**
 * 方法对应的栈帧
 */
public class Frame {
    // 本栈帧的下一个栈帧，这样方法的调用就成链了
    Frame lower;

    // 局部变量表
    private LocalVars localVars;

    // 操作数栈
    private OperandStack operandStack;

    // 栈帧所属的线程
    private Thread thread;

    // 程序计数器，即下一个要执行的指令码的位置
    private int nextPC;
//    private Method method;
    public Method method;


    public Frame(Thread thread, Method method) {
        this.thread = thread;
        this.method = method;
        this.localVars = new LocalVars(method.maxLocals);
        this.operandStack = new OperandStack(method.maxStack);
    }

    public Frame(Thread thread, int maxLocals, int maxStack) {
        this.thread = thread;
        this.localVars = new LocalVars(maxLocals);
        this.operandStack = new OperandStack(maxStack);
    }

    public Method method(){
        return this.method;
    }

    public void revertNextPC(){
        this.nextPC = this.thread.pc();
    }

    public LocalVars localVars() {
        return localVars;
    }

    public OperandStack operandStack() {
        return operandStack;
    }

    public Thread thread() {
        return this.thread;
    }

    public int nextPC() {
        return this.nextPC;
    }

    public void setNextPC(int nextPC) {
        this.nextPC = nextPC;
    }

}
